function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}


var serverUrl = 'https://api.douban.com';
function serverUrlFactory(url){
  return serverUrl+url;
}
function getMovieList (url, title, requestData) {
  var self = this;
  return new Promise((resolve, reject) => {
    wx.request({
      url: self.serverUrlFactory(url),
      method: 'GET',
      header: {
        "content-type": "json"
      },
      data: requestData,
      success: function (res) {
        console.log(res);
        res.data.title = title;
        res.data.url = url;
        resolve(res.data);
      },
      fail: function (err) {
        reject(err);
        console.log(err);
      }
    })
  })
};
function movieDataFactory(data) {
  var minData = [];
  for (var key in data.subjects) {
    minData.push({
      medium: data.subjects[key].images.medium,
      large: data.subjects[key].images.large,
      title: data.subjects[key].title,
      average: data.subjects[key].rating.average
    })
  }
  return {
    title: data.title,
    url:data.url,
    subjects: minData
  };
}
module.exports = {
  formatTime: formatTime,
  serverUrlFactory: serverUrlFactory,
  getMovieList: getMovieList,
  movieDataFactory: movieDataFactory
}
